package pl.nazaweb.imedproplus.rest;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import pl.nazaweb.imedproplus.business.service.UserService;
import pl.nazaweb.imedproplus.rest.pojo.ResponsePojo;
import pl.nazaweb.imedproplus.rest.pojo.UserPojo;

@Stateless
@Path("/json/user")
public class Users extends AbstractRest<UserPojo> {

    @EJB
    private UserService userService;

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public ResponsePojo getUserList() {
        return this.getEntityPojoList();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/create")
    public ResponsePojo createUsers(List<UserPojo> userPojos) {
        return this.createEntities(userPojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/destroy")
    public ResponsePojo removeUsers(List<UserPojo> userPojos) {
        return this.destroyEntities(userPojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/update")
    public ResponsePojo updateUsers(List<UserPojo> userPojos) {
        return this.updateEntities(userPojos);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/password")
    public ResponsePojo changePassword(@QueryParam("password1") String password, @QueryParam("userId") Long userId) {
        ResponsePojo response = new ResponsePojo();
        try {
            this.userService.changeUserPassword(userId, password);
            response.success = true;
        } catch (Exception e) {
            response.success = false;
            response.message = e.getMessage();
        }
        return response;
    }

    @Override
    public List<UserPojo> getEntityPojoListResult() {
        List<UserPojo> result = new ArrayList<UserPojo>();
        for (pl.nazaweb.imedproplus.model.entity.Users user : this.userService.getUsersList()) {
            result.add(new UserPojo(user));
        }
        return result;
    }

    @Override
    public void persistNewEnities(List<UserPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Users> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Users>();
        for (UserPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        try {
            this.userService.addNewUsers(result);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Users.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void removeEnities(List<UserPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Users> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Users>();
        for (UserPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.userService.removeUsers(result);
    }

    @Override
    public void updateEnities(List<UserPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Users> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Users>();
        for (UserPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.userService.updateUsers(result);
    }
}
